﻿using System.Windows;
using System.Windows.Controls;
using AvalonDock;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using RiskMan.DataClass.Models;
using System.Collections.ObjectModel;
using RiskMan.Views.Dialogs;
using RiskMan.Properties;
using System.Reflection;
using System.Text.RegularExpressions;

namespace RiskMan.Config
{
    /// <summary>
    /// Логика взаимодействия для UserControl1.xaml
    /// </summary>
    public partial class MainSettings : DocumentContent
    {
        private MainWindow mainWindow;

        public MainSettings()
        {
            try
            {
                this.mainWindow = RiskMan.MainWindow.StaticRiskMan;
                InitializeComponent();

         
                DataGrid_QuikItems.ItemsSource = mainWindow._dataProvider.T2QItems;

              }
            catch (Exception ex)
            {
                cfg.SetLog(ex.Message, MainWindow.StaticRiskMan.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
        }


        private List<String> GetListSeccode()
        {
            List<String> result = new List<string>() { cfg.Unknown };

            for (int i = 0; i < mainWindow._dataProvider.CurrentList.Count; i++)
                result.Add(mainWindow._dataProvider.CurrentList[i].Seccode);

            return result;
        }

      /// <summary>
        /// Показывает окно добавления нового Quik
        /// </summary>
        /// <param name="sender">Button</param>
        /// <param name="e"></param>
        private void Button_Add_Click(object sender, RoutedEventArgs e)
        {
            newQuikView nqv = new newQuikView(false, new QuikItem() { TradeList = new ObservableCollection<Trade>() }, mainWindow._dataProvider);
            nqv.ShowDialog();

            if (nqv.tmpQuik != null)
                mainWindow._dataProvider.T2QItems.Add((QuikItem)cfg.DeepClone(new QuikItem()
                {
                    Name = nqv.tmpQuik.Name,
                    Path = nqv.tmpQuik.Path,
                    TradeList = nqv.tmpQuik.TradeList,
                    Comments = nqv.tmpQuik.Comments
                }));

            DataGrid_QuikItems.IsReadOnly = false;
            DataGrid_QuikItems.ItemsSource = mainWindow._dataProvider.T2QItems;
            DataGrid_QuikItems.Items.Refresh();
            DataGrid_QuikItems.IsReadOnly = true;
        }

        private void button_Del_Click(object sender, RoutedEventArgs e)
        {
            if (DataGrid_QuikItems.SelectedIndex >= 0)
            {


                DataGrid_QuikItems.IsReadOnly = false;
                DataGrid_QuikItems.ItemsSource = mainWindow._dataProvider.T2QItems;
                DataGrid_QuikItems.Items.Refresh();
                DataGrid_QuikItems.IsReadOnly = true;
            }
            else
            {
                new DialogOkCancel("Выберите какой-нибудь из Quik", "Ошибка");
            }
        }

        /// <summary>
        /// Выводит информацию для редактирован ия
        /// </summary>
        /// <param name="sender">DataGrid</param>
        /// <param name="e"></param>
        private void DataGrid_QuikItems_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if ((sender as System.Windows.Controls.DataGrid).SelectedItems.Count == 1)
            {
                newQuikView nqv = new newQuikView(true, ((System.Windows.Controls.DataGrid)sender).SelectedItem as QuikItem, mainWindow._dataProvider);
                nqv.ShowDialog();

                if (nqv.tmpQuik != null)
                {
                    mainWindow._dataProvider.T2QItems[((System.Windows.Controls.DataGrid)sender).SelectedIndex] = new QuikItem()
                        {
                            Name = nqv.tmpQuik.Name,
                            Path = nqv.tmpQuik.Path,
                            Comments = nqv.tmpQuik.Comments,
                            TradeList = nqv.tmpQuik.TradeList
                        };
                }
                DataGrid_QuikItems.ItemsSource = mainWindow._dataProvider.T2QItems;
            }
        }

     

        #region Копирование строки

        private void DataGrid_QuikItems_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if ((sender as System.Windows.Controls.DataGrid).SelectedItems.Count == 1)
            {
                int index = (sender as System.Windows.Controls.DataGrid).SelectedIndex + 1;
                DataGrid_QuikItems.UnselectAll();
                if (new DialogYesNo("Вы действительно хотите скопировать строку " + index + "?", "Внимаение").Result == MessageBoxResult.Yes)
                {
                    var tmp = (QuikItem)cfg.DeepClone(mainWindow._dataProvider.T2QItems[index - 1 < 0 ? 0 : index - 1]);

                    string s = tmp.Name;
                    for (int j = 0; j < 10; j++)
                    {
                        bool tr = false;
                        for (int i = 0; i < mainWindow._dataProvider.T2QItems.Count; i++)
                            if (mainWindow._dataProvider.T2QItems[i].Name == s)
                            {
                                s += "0";
                                j = 0;
                                tr = true;
                                break;
                            }
                        if (tr == false)
                            break;
                    }
                    tmp.Name = s;
                    mainWindow._dataProvider.T2QItems.Add(tmp);
                }
            }
            else
            {

            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (DataGrid_QuikItems.SelectedItems.Count == 1)
            {
                int index = DataGrid_QuikItems.SelectedIndex + 1;
                DataGrid_QuikItems.UnselectAll();
                if (new DialogYesNo("Вы действительно хотите скопировать строку " + index + "?", "Внимаение").Result == MessageBoxResult.Yes)
                {
                    var tmp = (QuikItem)cfg.DeepClone(mainWindow._dataProvider.T2QItems[index - 1 < 0 ? 0 : index - 1]);

                    string s = tmp.Name;
                    for (int j = 0; j < 10; j++)
                    {
                        bool tr = false;
                        for (int i = 0; i < mainWindow._dataProvider.T2QItems.Count; i++)
                            if (mainWindow._dataProvider.T2QItems[i].Name == s)
                            {
                                s += "0";
                                j = 0;
                                tr = true;
                                break;
                            }
                        if (tr == false)
                            break;
                    }
                    tmp.Name = s;
                    mainWindow._dataProvider.T2QItems.Add(tmp);
                }
            }
            else
            {

            }
        }

        #endregion

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            if (DataGrid_QuikItems.SelectedItems.Count == 1)
            {
                mainWindow._dataProvider.T2QItems.RemoveAt(DataGrid_QuikItems.SelectedIndex);
            }
            DataGrid_QuikItems.ItemsSource = mainWindow._dataProvider.T2QItems;
        }

   }
}